package transactions.dq.historyarchives;

import transactions.dq.StringUtilForName;
import dinamica.*;

/**
 * @author anthony 2010-4-16
 * */
public class InsertMaster extends dinamica.audit.AuditableTransaction {
	public int service(Recordset inputParams) throws Throwable {
		// reuse superclass code
		int rc = super.service(inputParams);

		// get db object and save record with empty blob
		Db db = getDb();

		// 插入t_master
		String sqlInsertMaster = getResource("insert.sql");
		sqlInsertMaster = getSQL(sqlInsertMaster, inputParams);

		// 汉字名字转换成字母存放在master_chars
		String master_name = inputParams.getString("master_name");
		String charsStr = StringUtilForName.getFirstLetter(master_name);
		sqlInsertMaster = StringUtil.replace(sqlInsertMaster,
				"${master_chars}", charsStr);

		db.exec(sqlInsertMaster);

		// (2)插入t_master_detail
		String sqlInsertMasterDetail = getResource("insert_master-detail.sql");
		sqlInsertMasterDetail = getSQL(sqlInsertMasterDetail, inputParams);
		db.exec(sqlInsertMasterDetail);
		
		// (3)插入t_archives
		String sqlInsertArchives = getResource("insert_archives.sql");
		sqlInsertArchives = getSQL(sqlInsertArchives, inputParams);
		db.exec(sqlInsertArchives);
		
		String house_tuid = inputParams.getString("house_tuid");
		if(house_tuid != null && !"".equals(house_tuid)){//当楼源信息存在的情况下
			// (4)插入t_Placement(移至回迁安置界面维护)
			String sqlInsertPlacement = getResource("insert_placement.sql");
			sqlInsertPlacement = getSQL(sqlInsertPlacement, inputParams);
			db.exec(sqlInsertPlacement);

			// (5)修改t_house 状态
			String sqlUpdateHouse = getResource("update-house.sql");
			sqlUpdateHouse = getSQL(sqlUpdateHouse, inputParams);
			db.exec(sqlUpdateHouse);
		}

		//录入后自动弹出窗口所需数据
		String sqlBack = getResource("back.sql");
		Recordset rs = db.get(getSQL(sqlBack, inputParams));
		publish("backconfig", rs);

		return rc;

	}

}
